/**
 * 
 */
package com.moyq5.mvcmaker.impl;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.moyq5.mvcmaker.MvcMaker;
import com.moyq5.mvcmaker.WebModule;
import com.moyq5.mvcmaker.freemarker.FreeMarkerFactory;

import freemarker.core.ParseException;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;


/**
 * Controller类生成器
 * @author Moyq5
 * @date 2016年9月27日
 */
public class ControllerMvcMaker implements MvcMaker {

	private WebModule module;
	public ControllerMvcMaker(WebModule module) {
		this.module = module;
	}

	@Override
	public void execute(String entityName) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		
		String filePath = module.getPath() + File.separator + (module.getControllerPackage().replace(".", File.separator));
		File destFile = new File(filePath, entityName + "Controller.java");
		
		if (!destFile.exists()) {
			destFile.getParentFile().mkdirs();
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("entityName", entityName);
			model.put("cfg", module.project());
			FileWriter fw = new FileWriter(destFile);
			Template temp = FreeMarkerFactory.getCfg().getTemplate("controller.ftl");
			temp.process(model, fw);
		}
		
	}

}
